/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.octopus.optiworks.model;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Index;

import javax.persistence.*;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

@SuppressWarnings("serial")
@Entity
@Table
public class Employee implements Serializable{

    private Long id;
    private String username;
    private String email;
    private String password;
    private String testLightCode;
    private String serial;
    private String tel;
    private Set<EmployeeRole> roles = new HashSet<EmployeeRole>();

    @Id
	@GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    /**
     * Returns the username associated with this user account;
     *
     * @return the username associated with this user account;
     */
    @Basic(optional=false)
    @Column(length=100, unique=true)
    @Index(name="idx_user_username")
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Basic(optional=false)
    @Index(name="idx_user_email")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Returns the password for this user.
     *
     * @return this user's password
     */
    @Basic(optional=false)
    @Column(length=255)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(length=100)
    public String getTestLightCode() {
		return testLightCode;
	}

	public void setTestLightCode(String testLightCode) {
		this.testLightCode = testLightCode;
	}

    @Column(length=100)
	public String getSerial() {
		return serial;
	}

	public void setSerial(String serial) {
		this.serial = serial;
	}

    @Column(length=100)
	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	@ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name="user_role", joinColumns = { 
			@JoinColumn(name = "userid") }, inverseJoinColumns = { @JoinColumn(name = "roleid") })
    @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
    public Set<EmployeeRole> getRoles() {
        return roles;
    }

    public void setRoles(Set<EmployeeRole> roles) {
        this.roles = roles;
    }

    @Transient
    public Set<Long> getRolesId() {
    	Set<Long> rolesId = new HashSet<Long>();
    	Set<EmployeeRole> roles = this.getRoles();
    	if(roles != null){
    		for(EmployeeRole role : roles)
    			rolesId.add(role.getId());
    	}
        return rolesId;
    }

    public void setRolesId(Set<Long> rolesId) {
    	if(rolesId != null){
    		this.getRoles().clear();
    		for(Long roleId : rolesId){
    			EmployeeRole role = new EmployeeRole(roleId);
    			this.getRoles().add(role);
    		}
    	}
    }
    
    @Transient
    public Set<String> getPermissions(){
    	Set<String> set = new HashSet<String>();
    	if(roles != null){
    		for(EmployeeRole role : roles)
    			set.addAll(role.getPermissions());
    	}
    	return set;
    }
}


